import RPi.GPIO as GPIO
import time

IN1 = 5
IN2 = 6  # 树莓派的物理引脚与驱动模块连接
IN3 = 13
IN4 = 19

def setStep(h1, h2, h3, h4):
    GPIO.output(IN1, h1)
    GPIO.output(IN2, h2)
    GPIO.output(IN3, h3)
    GPIO.output(IN4, h4)

def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)  # 强制定义命名规则
    GPIO.setup(IN1, GPIO.OUT)  # 设置为输出模式
    GPIO.setup(IN2, GPIO.OUT)
    GPIO.setup(IN3, GPIO.OUT)
    GPIO.setup(IN4, GPIO.OUT)

def stop():
    setStep(0, 0, 0, 0)

def forward(delay, steps):  # A-B-C-D-A通电方式
    for i in range(0, steps):
        setStep(1, 0, 0, 0)
        time.sleep(delay)
        setStep(0, 1, 0, 0)
        time.sleep(delay)
        setStep(0, 0, 1, 0)
        time.sleep(delay)
        setStep(0, 0, 0, 1)
        time.sleep(delay)

def backward(delay, steps):
    for i in range(0, steps):
        setStep(0, 0, 0, 1)
        time.sleep(delay)
        setStep(0, 0, 1, 0)
        time.sleep(delay)
        setStep(0, 1, 0, 0)
        time.sleep(delay)
        setStep(1, 0, 0, 0)
        time.sleep(delay)

def loop():
    while True:
        backward(0.003, 512)  # 旋转1圈 循环512次
        stop()
        time.sleep(3)

if __name__ == '__main__':
    setup();
    forward(0.003,512);
















